package com.common.util.validators;

import java.util.Date;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;
import javax.faces.validator.FacesValidator;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

import org.apache.log4j.Logger;

import com.common.util.DateUtil;

@FacesValidator("MembershipValidityFromValidator")
public class MembershipValidityFromValidator implements Validator{

	private static Logger log = Logger.getLogger(MembershipValidityFromValidator.class);
	
	@Override
	public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
        Date validityFrom = (Date) value;
		UIInput dateComponent = (UIInput)component.getAttributes().get("effectiveToAtt");
		Date validityTo = (Date)dateComponent.getValue();

    	if(validityTo != null){
            if (!validityFrom.before(validityTo)) {
    			FacesMessage msg = new FacesMessage("Effective From. Validation Failed.", "Effective from is after effective to");
    			msg.setSeverity(FacesMessage.SEVERITY_ERROR);
    			throw new ValidatorException(msg);
            }
    	}
		
	}
	
}

